/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.drakeet.multitype.sample.utils;

import me.drakeet.multitype.sample.ResourceTable;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;

/**
 * ViewCache
 *
 */
public class ViewCache {
    /**
     * WIDTH
     */
    private static final int WIDTH = 2;

    /**
     * PARAM
     */
    private static final int FIVE = 5;

    /**
     * MAIN VIEW
     */
    private Component baseView;

    /**
     * TEXT VIEW
     */
    private Text text;

    /**
     * TEXT VIEW
     */
    private Text textNormal;

    /**
     * TEXT VIEW
     */
    private Text textLast;

    /**
     * IMAGE VIEW
     */
    private Image image;

    /**
     * IMAGE VIEW
     */
    private Image imageOuterNormal;

    /**
     * DirectionalLayout
     */
    private DirectionalLayout directionalLayout;

    /**
     * DirectionalLayout
     */
    private DirectionalLayout directionalLayoutLine;

    /**
     * ViewCache
     *
     * @param baseView Component
     */
    public ViewCache(Component baseView) {
        this.baseView = baseView;
    }

    /**
     * getTextView
     *
     * @return Text
     */
    public Text getTextView() {
        if (text == null) {
            if (baseView.findComponentById(ResourceTable.Id_text_item) instanceof Text) {
                text = (Text) baseView.findComponentById(ResourceTable.Id_text_item);
                ShapeElement drawable = new ShapeElement();
                drawable.setShape(ShapeElement.RECTANGLE);
                drawable.setStroke(WIDTH, RgbColor.fromArgbInt(Color.BLACK.getValue()));
                text.setBackground(drawable);
            }
        }
        return text;
    }

    /**
     * getNormalTextView
     *
     * @return Text Text value
     */
    public Text getNormalTextView() {
        if (textNormal == null) {
            textNormal = (Text) baseView.findComponentById(ResourceTable.Id_text_normal);
            ShapeElement drawable = new ShapeElement();
            drawable.setShape(ShapeElement.RECTANGLE);
            drawable.setStroke(WIDTH, RgbColor.fromArgbInt(Color.BLACK.getValue()));
            textNormal.setBackground(drawable);
        }
        return textNormal;
    }

    /**
     * getImageView
     *
     * @return Image
     */
    public Image getImageView() {
        if (image == null) {
            image = (Image) baseView.findComponentById(ResourceTable.Id_item_icon);
            ShapeElement drawable = new ShapeElement();
            drawable.setShape(ShapeElement.RECTANGLE);
            drawable.setStroke(WIDTH, RgbColor.fromArgbInt(Color.BLACK.getValue()));
        }
        return image;
    }

    /**
     * getImageOuterNormalView
     *
     * @return Image
     */
    public Image getImageOuterNormalView() {
        if (imageOuterNormal == null) {
            imageOuterNormal = (Image) baseView.findComponentById(ResourceTable.Id_image_outer_normal);
            ShapeElement drawable = new ShapeElement();
            drawable.setShape(ShapeElement.RECTANGLE);
            drawable.setStroke(FIVE, RgbColor.fromArgbInt(Color.BLACK.getValue()));
        }
        return imageOuterNormal;
    }

    /**
     * getDirectionalLayout
     *
     * @param resId Id
     */
    public void getDirectionalLayout(int resId) {
        if (directionalLayout == null) {
            directionalLayout = (DirectionalLayout) baseView.findComponentById(resId);

            DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(
                    DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
            layoutConfig.alignment = LayoutAlignment.CENTER;
            directionalLayout.setLayoutConfig(layoutConfig);

            ShapeElement drawable = new ShapeElement();
            drawable.setShape(ShapeElement.RECTANGLE);
            drawable.setStroke(WIDTH, RgbColor.fromArgbInt(Color.BLACK.getValue()));
            directionalLayout.setBackground(drawable);
            directionalLayout = null;
        }
    }

    /**
     * getLastTextView
     *
     * @return Text
     */
    public Text getLastTextView() {
        if (textLast == null) {
            textLast = (Text) baseView.findComponentById(ResourceTable.Id_last_text);
        }
        return textLast;
    }

    /**
     * getDirectionalLayoutLine
     */
    public void getDirectionalLayoutLine() {
        if (directionalLayoutLine == null) {
            directionalLayoutLine = (DirectionalLayout) baseView.findComponentById(ResourceTable.Id_dl_payload_main);

            DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(
                    DirectionalLayout.LayoutConfig.MATCH_PARENT, FIVE);
            layoutConfig.alignment = LayoutAlignment.CENTER;
            directionalLayoutLine.setLayoutConfig(layoutConfig);

            ShapeElement drawable = new ShapeElement();
            drawable.setShape(ShapeElement.LINE);
            drawable.setStroke(WIDTH, RgbColor.fromArgbInt(Color.BLACK.getValue()));
            directionalLayoutLine.setBackground(drawable);
            directionalLayoutLine = null;
        }
    }
}